package com.tz.cartelo.activity;

import android.content.Intent;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.tz.cartelo.R;
import com.tz.cartelo.constant.Api;
import com.tz.cartelo.constant.Key;
import com.tz.cartelo.model.DataParams;
import com.tz.cartelo.model.Login;
import com.tz.cartelo.result.ResultLogin;
import com.tz.common.util.LogUtil;
import com.tz.common.util.PreferencesUtils;

/**
 * Created by Administrator on 2017/4/15.
 */

public class LoginActivity extends DataLoadActivity implements View.OnClickListener {
    private static final String TAG = "LoginActivity";
    private EditText mLoginPhoneEdit, mLoginPasswordEdit;
    private ImageView mLoginEyeImg;
    private Api mApi;
    private boolean mIsSeePass;
//    private ResultLogin mResultLogin;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_login;
    }

    @Override
    protected void init() {
        mLoginPhoneEdit = (EditText) findViewById(R.id.login_phone_edit);
        mLoginPasswordEdit = (EditText) findViewById(R.id.login_password_edit);
        mLoginEyeImg = (ImageView) findViewById(R.id.login_see_pass);
        mLoginEyeImg.setOnClickListener(this);
        findViewById(R.id.login_btn).setOnClickListener(this);
        findViewById(R.id.login_forget_text).setOnClickListener(this);
    }

    @Override
    protected void initTitle() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login_see_pass:
                if (mIsSeePass) {
                    mLoginPasswordEdit.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    mIsSeePass = false;
                    mLoginEyeImg.setImageResource(R.mipmap.ic_eye_open);
                } else {
                    mLoginPasswordEdit.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    mIsSeePass = true;
                    mLoginEyeImg.setImageResource(R.mipmap.ic_eye_close);
                }
                break;
            case R.id.login_btn:
                if (mLoginPhoneEdit.getText().toString().trim().isEmpty()) {
                    showToast(R.string.login_phone_empty);
                }
//                else if (!CommonUtils.isPhoneNum(mLoginPhoneEdit.getText().toString().trim())) {
//                    showToast(R.string.login_wrong_phone);
//                }
                else if (mLoginPasswordEdit.getText().toString().trim().isEmpty()) {
                    showToast(R.string.login_password_hint);
                } else {
                    mApi = Api.LOGIN;
                    loadData(mApi, true);
                }
                break;
            case R.id.login_forget_text:
                switchActivityForResult(SafeTestActivity.class, Key.FORGET_FLAG, null);
                break;
            default:
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case Key.REGISTER_FLAG:
                case Key.FORGET_FLAG:
                    mLoginPhoneEdit.setText("");
                    mLoginPasswordEdit.setText("");
                    break;
                default:
                    break;
            }
        }
    }

    @Override
    protected void initParams(DataParams params) {
        switch (mApi) {
            case LOGIN:
                params.addParam("username", mLoginPhoneEdit.getText().toString().trim());
                params.addParam("password", mLoginPasswordEdit.getText().toString().trim());
                break;
            default:
                break;
        }
    }

    @Override
    protected void disposeResult(Api api, String response) {
        LogUtil.d(TAG, response);
        switch (api) {
            case LOGIN:
                ResultLogin mResultLogin = fromJson(response, ResultLogin.class);
                if (!mResultLogin.isSuccess()) {
                    showToast(mResultLogin.getMessage());
                    return;
                }
                showToast(R.string.login_succeed);
                saveLoginData(mResultLogin.getLogin());
                break;
            default:
                break;
        }
    }

    private void saveLoginData(Login loginData) {
        PreferencesUtils.putString(Key.PRE_USER_SEX, loginData.getSex());
        PreferencesUtils.putString(Key.PER_USER_PHONE, loginData.getMobilePhone());
        PreferencesUtils.putString(Key.PER_USER_EMAIL, loginData.getEmail());
        PreferencesUtils.putString(Key.PER_USER_NICK, loginData.getNickName());
        PreferencesUtils.putString(Key.PER_USER_COMPANY, loginData.getCompany());
        PreferencesUtils.putString(Key.PER_USER_ID, loginData.getUserId() + "");
        PreferencesUtils.putInt(Key.PER_USER_GRADE, loginData.getGrade());
        PreferencesUtils.putString(Key.PER_USER_HEAD, loginData.getHeadPortrait());
        PreferencesUtils.putString(Key.PER_USER_NAME, loginData.getUserName());
        PreferencesUtils.putInt(Key.PER_USER_TYPE, loginData.getUserType());
        PreferencesUtils.putBoolean(Key.PER_LOGIN, true);
        switchActivityAndFinish(MainActivity.class, null);
    }

}
